package common.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.yuntsg.ruiijn.database_entity.Pubmed;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PubmedUtil {
    public static void main(String[] args) {
        Pubmed pubmedInfo = getPubmedInfo("26192728");
        System.out.println(pubmedInfo);
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file("E:\\PMIDS.xlsx"));
        List<List<Object>> read = reader.read();
        for (List<Object> objects : read) {
            for (Object object : objects) {
                System.out.println(object);
            }
        }
    }


    public static Pubmed getPubmedInfo(String term) {

        try {
            System.out.println("准备通过Pubmed的接口获取全文的检索词___" + term);
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("term", URLEncoder.encode(term, "utf-8"));
            String result = HttpRequest.post("http://207.231.108.249:808/pm-newsearch.php")
                    .header(Header.USER_AGENT, "Hutool http")//头信息，多个头信息多次调用此方法即可
                    .form(paramMap)//表单内容
                    .timeout(30000)//超时，毫秒
                    .execute().body();


            JSONObject jsonObject = JSONUtil.parseObj(result);
            System.out.println(jsonObject);
            if (jsonObject.get("type").equals(1)) {
                JSONObject info = JSONUtil.parseObj(JSONUtil.parseArray(jsonObject.getStr("list")).get(0));
                String pmid = info.getStr("pmid");


                String title = "";
                String source = "";
                String doi = "";
                String year = "";
                String author = "";
                try {
                    title = info.getStr("title");
                    title = title.replaceAll("</?[^>]+>", "");
                } catch (Exception ex) {
                }
                try {
                    source = info.getStr("source");
                    source = source.split("Add to Search.")[1];
                    try {
                        source = source.split("doi:")[0];
                    } catch (Exception e) {
                    }
                    source = source.trim();
                } catch (Exception ex) {
                }
                //todo
                try {
                    doi = info.getStr("doi");
                } catch (Exception ex) {
                }
                try {
                    year = info.getStr("year");
                } catch (Exception ex) {
                }
                try {
                    author = JSONUtil.parseObj(JSONUtil.parseArray(info.getStr("author")).get(0)).getStr("nameiso") + ", ";
                    try {
                        author += JSONUtil.parseObj(JSONUtil.parseArray(info.getStr("author")).get(1)).getStr("nameiso") + ", ";
                        author += JSONUtil.parseObj(JSONUtil.parseArray(info.getStr("author")).get(2)).getStr("nameiso") + "...";
                    } catch (Exception e) {
                    }

                } catch (Exception ex) {
                }

                Pubmed pubmed = new Pubmed();
                pubmed.setPmid(pmid);
                pubmed.setAu(author);
                pubmed.setDoi(doi);
                pubmed.setSo(source);
                pubmed.setTi(title);
                pubmed.setYear(year);

                return pubmed;

            } else if (jsonObject.get("type").equals(2)) {

                System.out.println("通过Pubmed接口获取到的type为2");
                JSONObject info = JSONUtil.parseObj(JSONUtil.parseArray(jsonObject.getStr("list")).get(0));
                String pmid = info.getStr("pmid");


                String title = "";
                String source = "";
                String doi = "";
                String year = "";
                String author = "";

                try {
                    title = info.getStr("title");
                    title = title.replaceAll("</?[^>]+>", "");
                    System.out.println("通过Pubmed接口获取到的type为2的标题为___" + title);
                } catch (Exception ex) {
                }
                try {
                    source = info.getStr("source");
                    source = source.split("Add to Search.")[1];
                    try {
                        source = source.split("doi:")[0];
                    } catch (Exception e) {
                    }
                } catch (Exception ex) {
                }
                //todo
                try {
                    doi = info.getStr("doi");
                } catch (Exception ex) {
                }
                try {
                    year = info.getStr("year");
                } catch (Exception ex) {
                }
                try {
                    author = JSONUtil.parseObj(JSONUtil.parseArray(info.getStr("author")).get(0)).getStr("nameiso") + ", ";
                    try {
                        author += JSONUtil.parseObj(JSONUtil.parseArray(info.getStr("author")).get(1)).getStr("nameiso") + ", ";
                        author += JSONUtil.parseObj(JSONUtil.parseArray(info.getStr("author")).get(2)).getStr("nameiso") + "...";
                    } catch (Exception e) {
                    }

                } catch (Exception ex) {
                }

                Pubmed pubmed = new Pubmed();
                pubmed.setPmid(pmid);
                pubmed.setAu(author);
                pubmed.setDoi(doi);
                pubmed.setSo(source);
                pubmed.setTi(title);
                pubmed.setYear(year);

                return pubmed;

            } else {
                return null;
            }
        } catch (
                Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }


}
